package com.fitness.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.MemberConvertor;
import com.fitness.front.exception.BizException;
import com.fitness.front.modal.course.params.CourseReserveQueryParams;
import com.fitness.front.modal.course.vo.CourseReserveVO;
import com.fitness.front.modal.course.vo.CourseScheduleVO;
import com.fitness.front.modal.member.dto.MemberDto;
import com.fitness.front.modal.member.entity.Member;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.*;
import com.fitness.front.utils.EncryptUtil;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/profile")
public class ProfileController {

    private final ICourseScheduleService courseScheduleService;
    private final ICourseReserveService courseReserveService;
    private final IMemberService memberService;
    private final MemberConvertor memberConvertor;

    @GetMapping("/myCourses")
    public ServerResponseEntity<List<CourseScheduleVO>> myCourses(@RequestParam("memberId") Long memberId,
                                                          @RequestParam("yearMonth") String yearMonth){
        List<CourseScheduleVO> retList = courseScheduleService.getCourseSchedulesByMemberIdAndYearMonth(memberId,yearMonth);
        return ServerResponseEntity.success(retList);
    }

    @GetMapping("/myCourseSchedules")
    public ServerResponseEntity<List<String>> myCoursesSchedules(@RequestParam("memberId") Long memberId,
                                                                  @RequestParam("yearMonth") String yearMonth){
        List<String> retList = courseScheduleService.getCourseScheduleStartDate(memberId,yearMonth);
        return ServerResponseEntity.success(retList);
    }

    /**
     * 分页获取课程预约列表
     */
    @GetMapping("/myCourseReserve")
    public ServerResponseEntity<IPage<CourseReserveVO>> myCourseReserve(CourseReserveQueryParams queryParams) {
        Long memberId = queryParams.getMemberId();
        Page<CourseReserveVO> result = courseReserveService.getCourseReservePage(memberId,queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 获取课程安排详情
     */
    @GetMapping("/myCourseSchedule")
    public ServerResponseEntity<List<CourseScheduleVO>> myCourseSchedule(String date) {
        List<CourseScheduleVO> result = courseScheduleService.getCourseScheduleDetail(date);
        return ServerResponseEntity.success(result);
    }

    /**
     * 获取课程安排详情
     */
    @SaCheckLogin
    @PutMapping("/modifyPassword")
    public ServerResponseEntity<Boolean> modifyPassword(@Param("oldPassword") String oldPassword,
                                                                       @Param("newPassword") String newPassword,
                                                                       @Param("confirmPassword") String confirmPassword) {
        if(StrUtil.isBlank(oldPassword) || StrUtil.isBlank(newPassword) || StrUtil.isBlank(confirmPassword) || !newPassword.equals(confirmPassword)){
            return ServerResponseEntity.fail("MP00001","密码为空或者新密码与确认密码不一样");
        }
        String loginId = StpUtil.getLoginIdAsString();
        String[] uidAndUname = loginId.split("_");
        Long uid = Long.parseLong(uidAndUname[0]);
        Member member = memberService.getById(uid);
        if(member == null){
            return ServerResponseEntity.fail("MP00001","用户不存在");
        }

        if(!EncryptUtil.matchPassword(oldPassword,member.getSalt(),member.getPassword())){
            return ServerResponseEntity.fail("MP00001","密码不正确");
        }
        return ServerResponseEntity.success(memberService.modifyPass(uid,member.getSalt(),newPassword));
    }

    /**
     * 获取课程安排详情
     */
    @SaCheckLogin
    @PutMapping("/modifyMemberInfo")
    public ServerResponseEntity<Boolean> modifyMemberInfo(@RequestBody MemberDto memberDto) {
        String username = memberDto.getUsername();
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getUsername,username));
        if(member != null){
            return ServerResponseEntity.fail("M00002","用户名已存在");
        }
        return ServerResponseEntity.success(memberService.updateById(memberConvertor.dto2Entity(memberDto)));
    }

}
